﻿///<reference path="../../scripts/jquery-1.4.4.min.js" />
///<reference path="../../scripts/kooboo/kooboo.form.js" />
///<reference path="../../scripts/kooboo/kooboo.mvcvalidation.js" />

$(function () {

    //render address list
    var isEven = false;
    for (var i = 0; i < address_list.length; i++) {
        var item = address_list[i];

        var row = RenderAddress(item);

        if (isEven) {
            row.addClass("even");
        } else {
            row.removeClass("even");
        }

        isEven = !isEven;

    }

    //add address
    $(".address-add").click(function (e) {
        e.preventDefault();
        var item = {
            Id: -1,
            Address2: "",
            Address1: "",
            Postcode: "",
            City: "",
            State: "",
            CountryName: "",
            FirstName: "",
            LastName: "",
            Phone: ""
        };

        EditForAddress(item);

        $("#address-form").attr("action", address_add_action);

        return false;
    });

    //delete address
    $(".delete-link").live("click", function (e) {
        e.preventDefault();

        if (confirm($(this).attr("message"))) {
            $(this).parents("form:first").submit();
        }
        return false;
    });

    $(".address-delete-form").live("submit", function (e) {
        e.preventDefault();
        var row = $(this).parents("tr:first")
        $(this).ajaxPost({
            success: function () {
                row.remove();
            }
        });
        return false;
    });


    //edit address
    $(".edit", "#address-container").live("click", function (e) {
        e.preventDefault();

        $("#address-form").attr("action", address_update_action);

        var row = $(this).parent().parent();
        var address = row.find("td:first");

        var item = {
            Id: row.attr("addr-id"),
            Address2: $.trim(address.find(".Address2").html()),
            Address1: $.trim(address.find(".Address1").html()),
            Postcode: $.trim(address.find(".Postcode").html()),
            City: $.trim(address.find(".City").html()),
            State: $.trim(address.find(".State").html()),
            CountryCode: address.find("input[name=CountryCode]").val(),
            CountryName: $.trim(address.find(".CountryName").html()),
            FirstName: $.trim(address.find(".FirstName").html()),
            LastName: $.trim(address.find(".LastName").html()),
            Phone: $.trim(address.find(".Phone").html())
        };

        EditForAddress(item);


        return false;
    });

    //dialog cancel
    $("#address-form-dialog").find(".cancel").click(function (e) {
        e.preventDefault();
        $("#address-form-dialog").dialog("close");
        return false;
    });

    //update
    $("#address-form").submit(function (e) {
        e.preventDefault();
        var form = $(this);
        $(this).ajaxPost({
            success: function (id) {
                var item = {
                    Id: id,
                    Address2: form.find("input[name=Address2]").val(),
                    Address1: form.find("input[name=Address1]").val(),
                    Postcode: form.find("input[name=Postcode]").val(),
                    City: form.find("input[name=City]").val(),
                    State: form.find("input[name=State]").val(),
                    CountryName: form.find("select[name=Country.CountryCode]>option:selected").text(),
                    FirstName: form.find("input[name=FirstName]").val(),
                    LastName: form.find("input[name=LastName]").val(),
                    Phone: form.find("input[name=Phone]").val()
                };
                RenderAddress(item);
                $("#address-form-dialog").dialog("close");
            },
            box: $("#address-form-dialog")
        });
    });


    //helper

    function EditForAddress(item) {
        var form = $("#address-form");
        form.cleanError(form);
        form.find("input[name=Id]").val(item.Id);
        form.find("input[name=Address2]").val(item.Address2);
        form.find("input[name=Address1]").val(item.Address1);
        form.find("input[name=Postcode]").val(item.Postcode);
        form.find("input[name=City]").val(item.City);
        form.find("input[name=State]").val(item.State);

        form.find("select>option").each(function (index, option) {
            var $item = $(option);
            $item.attr("selected", $item.text() == item.CountryName);
        });

        form.find("input[name=FirstName]").val(item.FirstName);
        form.find("input[name=LastName]").val(item.LastName);
        form.find("input[name=Phone]").val(item.Phone);


        $("#address-form-dialog").dialog('open');

    }


    function RenderAddress(item) {
        var container = $("#address-container");
        var row = container.find("tr[addr-id=" + item.Id + "]");
        if (row.size() == 0) {

            row = $("#address-row").find(".template").clone();
            row.find("input[name=Id]").val(item.Id);
            row.attr("addr-id", item.Id);
            container.append(row);
        }

        var address = row.find("td:first");

        address.find(".Address2").html(item.Address2);
        address.find(".Address1").html(item.Address1);
        address.find(".Postcode").html(item.Postcode);
        address.find(".City").html(item.City);
        address.find(".State").html(item.State);
        address.find(".CountryName").html(item.CountryName);
        address.find("input[name=CountryCode]").val(item.CountryCode);
        address.find(".FirstName").html(item.FirstName);
        address.find(".LastName").html(item.LastName);
        address.find(".Phone").html(item.Phone);

        return row;

    }
});